<?php

namespace Admin\Controller;
use Think\Controller;

class AjaxController extends Controller{
	
	
	/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/


	public function getRegion(){
		// $Region=M("region");
		// $map['pid']=$_REQUEST["pid"];
		// $map['type']=$_REQUEST["type"];
		// $list=$Region->where($map)->select();
		// echo json_encode($list);
	}

  public function getTree(){
$userid=I('post.userid');
    
    $base = $this->getTreeBaseInfo ( $userid );
    $znote = $this->getTreeInfo ($userid);
    $znote [] = $base;

  
    echo json_encode ( array ("status" => 0,"data" => $znote ) );;
  }

  public function getTreeBaseInfo($id='')
  {
      if (! $id)
      return;
    $r = M ( "user" )->find($id);
    $jbset=M('jbset')->find();
    $jb='jb'.$r['status'];
    if ($r)
      return array (
          "id" => $r ['id'],
          "pId" => $r ['invite_id'],
          "name" => '级别:'.$jbset[$jb].'  '.$r ['mobile'] . "[" . $r ['truename'] . "]"."   个人总业绩：".$r['ordernums']." 团队总业绩:  ".$r['team_ordernums']
      );
    return;
  }
  public function getTreeInfo($id) {
    static $trees = array ();
    $ids = self::get_childs ( $id );
    if (! $ids){
      return $trees;
    }
  
    $_SESSION['user_jb']++;
    //echo $_SESSION['user_jb'].'<br>';
    foreach ( $ids as $v ) {
      if(!empty($v))
      {
        $trees [] = $this->getTreeBaseInfo ( $v );
        $this->getTreeInfo ( $v );
      }
  
    }
    return $trees;
  }

   public function getTreeBaseInfoHome($id='')
  {
      if (! $id)
      return;
    $r = M ( "user" )->find($id);
    $jbset=M('jbset')->find();
    $jb='jb'.$r['status'];
    if ($r)
      return array (
          "id" => $r ['id'],
          "pId" => $r ['invite_id'],
          "mobile"=>$r ['mobile'],
          "truename"=>$r ['truename'],
          "jb" => $jbset[$jb],
          "ordernums"=>$r['ordernums'],
          "team_ordernums"=>$r['team_ordernums'],
          "ctime"=>$r['ctime'],
      );
      //return $r;
    return;
  }
  public function getTreeInfoHome($id) {
    static $trees = array ();
    $ids = self::get_childs ( $id );
    if (! $ids){
      return $trees;
    }
  
    $_SESSION['user_jb']++;
    //echo $_SESSION['user_jb'].'<br>';
    foreach ( $ids as $v ) {
      if(!empty($v))
      {
        $trees [] = $this->getTreeBaseInfoHome ( $v );
        $this->getTreeInfoHome ( $v );
      }
  
    }
    return $trees;
  }


  


    public static function get_childs($id) {
  
    if (! $id)
      return null;
  
    $childs_id = array ();
    $childs = M ( "user" )->field ( "id" )->where ( array (
        'invite_id' => $id
    ) )->select ();
  
    foreach ( $childs as $v ) {
      $childs_id [] = $v ['id'];
    }
  
    if ($childs_id)
      return $childs_id;
    return 0;
  }
	
}
